<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/11
 * Time: 10:54 AM
 */
class PHZ_Mopai
{
    /**
     * 摸牌起手后的跑牌检查
     * 解释：本方法为玩家自己摸了一个牌；刚好手里又有三张一摸一样的牌的时候；称之为摸牌起跑
     * @param $my_card_info   摸牌人当前手中的牌信息
     * @param $uid            摸牌人ID
     * @param $card_number    摸牌号
     * @param $db_connect     数据库链接
     */
    public static function __mopai_ziji_qipao( $room_info , $my_card_info , $mop_uid , $card_number , $db_connect ){
        //查询手中共有几张相同的手牌，等于3张才能构成摸牌起跑
        $find = 0;
        $temp = explode(",",$my_card_info['sp']);
        foreach( $temp as $k1 => $v1 ){if( $card_number == $v1 ){$find++;}}
        //等于3张的时候才继续执行下去
        if( $find < 3 ){
            return array('status'=>false , 'uid' => 0);
        }
        //是否重跑？.重跑，下家出牌；非重跑；自家出牌
        $is_chongpao = 0;
        if( $my_card_info['ti_pao'] >= 1 ){
            $is_chongpao = 1;
        }
        //删除自家的手牌信息并更新“手牌，下牌” - 删除基本就是删除所有了；一种牌最多3张了；下牌就直接在已经下的牌后面追加4个牌就好了；；
        foreach ( $temp as $key => $value ){
            if( $value == $card_number ){unset( $temp[$key] );}
        }
        $update_sp = implode(",",$temp);
        $update_xp = $my_card_info['xp'] . $card_number . "_" . $card_number . "_" . $card_number . "_" . $card_number . ",";
        $db_connect->update('tt_room_card')->cols(array('sp'=> $update_sp ,'xp'=> $update_xp , 'ti_pao' => ($my_card_info['ti_pao']+1)   ))->where('id=' . $my_card_info['id'] )->query();
        //增加息数（摸牌起提-大12，小9）
        PHZ_SuanXi::__xi( $room_info , $mop_uid , "mop_ti" , $card_number , $db_connect );
        //插入提牌数据
        PHZ_Card::__addTipai( $room_info['room_id'] , $room_info['room_js'] , $mop_uid , $card_number , $db_connect );
        //给所有人广播摸牌起跑的信息
        $temp = array();
        $temp['pl']          = 15;//15为自己摸牌，自己起跑了....
        $temp['card_number'] = $card_number;
        $temp['put_uid']     = $is_chongpao == 1 ? 0 : $mop_uid;//重跑丢失出牌权；非重跑出牌权为自身
        $temp['mop_uid']     = 0;//全部人丧失摸牌权
        $temp['is_chong']    = $is_chongpao;//是否是重跑
        PHZ_Log::__send_user_log( $room_info , $mop_uid , $temp  , $db_connect );
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
        //返回允许跑牌信息
        return array('status' => true , 'uid' => $mop_uid , 'is_chongpao' => $is_chongpao );
    }


    /**
     * 用户摸牌后；判断自身是否能够畏
     * @param $room_info
     * @param $my_card_info
     * @param $mop_uid
     * @param $card_number
     * @param $db_connect
     * @return bool
     */
    public static function __mopai_chk_wei( $room_info , $my_card_info ,$mop_uid , $card_number ,  $db_connect ){
        $find = 0;
        $temp = explode(",",$my_card_info['sp']);
        //BUG:如果只剩最后2个张牌的情况下；不允许进行畏牌操作；（畏牌不够息不能进行最后2张牌的畏）
        if( count( $temp ) == 2 && $temp[0] == $temp[1] ){
            $default_huxi = PHZ_Tools::__get_user_xpAndKanzi_Huxi( $room_info['room_id'] ,$mop_uid  );//查找已经存在的息数
            $thiswei_huxi = $card_number <= 10 ? 3 : 6;
            if( ($default_huxi + $thiswei_huxi) < MININUM_HUXI ){
                return false;
            }
        }
        //遍历查牌
        foreach( $temp as $k1 => $v1 ){if( $card_number == $v1 ){$find++;}}
        if( $find == 2 ){
            //进行自家(畏牌人)的手牌以及下牌操作
            $my_sp = $my_card_info['sp'];
            $my_sp = explode(",",$my_sp);
            foreach ($my_sp as $key => $value){
                if( $value == $card_number ){unset( $my_sp[$key] );}
            }
            $my_card_info['xp'] = $my_card_info['xp'] . $card_number . "_" . $card_number . "_" . $card_number . ",";
            //添加一个card_wei；用于后面的检查摸牌下跑操作
            $card_wei           = $my_card_info['card_wei'] . $card_number . ",";
            //进行数据库操作
            $db_connect->update('tt_room_card')->cols(array('card_wei'=> $card_wei ,'sp'=> implode(",",$my_sp) ,'xp'=> $my_card_info['xp']   ))->where('id=' . $my_card_info['id'] )->query();
            //增加息数（摸牌畏牌-大6，小）
            PHZ_SuanXi::__xi( $room_info , $mop_uid , "mop_wei" , $card_number , $db_connect );
            //是否臭畏，臭畏即之前可碰没有碰，现在自己畏了的牌；则为臭畏
            $chou_wei = 0;
            if( PHZ_User::__Peng_card_is_Out( $room_info , $mop_uid , $card_number , $db_connect ) == false ) {
                $chou_wei = 1;
            }
            //广播"摸牌存畏"的信息
            $temp = array();
            $temp['pl']          = 12;//12为摸牌畏牌，畏牌是全局广播，所以所有人都会看到
            $temp['card_number'] = $card_number;//牌号
            $temp['put_uid']     = $mop_uid;//摸牌人获得出牌权
            $temp['mop_uid']     = 0;//全部人丧失摸牌权
            $temp['chou_wei']    = $chou_wei;
            PHZ_Log::__send_user_log( $room_info , $mop_uid , $temp  , $db_connect );
            //设置出牌权
            PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
            return true;
        }
        return false;
    }

    //##########################################################废弃￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥、、、、、/////
    //##########################################################废弃￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥、、、、、/////
    /**
     * 摸牌起手后的跑牌检查(下跑检查) - 自己碰过的牌，或者畏过的牌；够成摸牌下跑
     * 解释：本方法为玩家自己摸了一个牌；刚好手里又有三张一摸一样的牌的时候；称之为摸牌起跑
     * 注意：这里要区分是碰牌的下跑，还是畏牌的下跑；畏牌的下跑只能自己看到牌；碰牌的下跑，所有人都能看到牌
     * @param $my_card_info   摸牌人当前手中的牌信息
     * @param $uid            摸牌人ID
     * @param $card_number    摸牌号
     * @param $db_connect     数据库链接
     */
    public static function __mopai_ziji_xiapao( $room_info , $my_card_info , $mop_uid , $card_number , $db_connect ){
        //通过摸进来的牌的名称，组合成允许下跑的牌面
        $xiapao_card = $card_number . "_" . $card_number . "_" . $card_number;
        //查询手里的下牌中；是否存在3张的组合牌
        $find = 0;
        $temp = explode(",",$my_card_info['xp']);
        foreach( $temp as $k1 => $v1 ){
            if( $v1 == $xiapao_card ){
                $find++;
            }
        }
        //因为下跑有的话,顶多也就能找到一个了
        if( $find == 0 ){
            return array('status'=>false , 'uid' => 0);
        }
        //是否重跑？.重跑，自动下家出牌；非重跑；自家出牌
        $is_chongpao     = 0;
        if( $my_card_info['ti_pao'] >= 1 ){
            $is_chongpao = 1;
        }
        //更新自己的下牌信息；因为是摸牌起“下跑”；不需要更新手牌信息；只需要更新下牌就可以了；直接将3牌替换为4牌
        $update_xp = str_replace( $xiapao_card , $xiapao_card . "_" . $card_number , $my_card_info['xp'] );
        $db_connect->update('tt_room_card')->cols(array('xp'=> $update_xp , 'ti_pao' => ($my_card_info['ti_pao']+1)   ))->where('id=' . $my_card_info['id'] )->query();
        //查询是否畏的，还是跑的？？？？ - 因为有下牌；所以只需要查碰就行了，不是碰就肯定是畏；要是碰畏都没有，那就出事了
        $card_peng = $my_card_info['card_peng'];$card_peng = explode(",",$card_peng);
        $is_peng   = false;
        foreach ( $card_peng as $key => $value ){
            if( $value == $card_number ){
                $is_peng = true;break;
            }
        }
        //给所有人广播摸牌起下跑的信息
        $temp = array();
        $temp['pl']          = 16;//15为自己摸牌，自己又存在碰过的或者畏过的
        $temp['card_number'] = $card_number;
        $temp['put_uid']     = 0;//全部人丧失出牌权
        $temp['mop_uid']     = 0;//全部人丧失摸牌权
        $temp['is_chong']    = $is_chongpao;//是否是重跑
        $temp['pai_type']    = $is_peng == true ? 'peng' : 'wei';//返回这个牌是碰的还是畏出来的
        PHZ_Log::__send_user_log( $room_info , $mop_uid , $temp  , $db_connect );
        //设置出牌权
        PHZ_Room::__setThisPutUser( $room_info['room_id'] , $temp['put_uid'] , $db_connect );
        //返回允许跑牌信息
        return array('status' => true , 'uid' => $mop_uid , 'is_chongpao' => $is_chongpao );
    }
    //##########################################################废弃￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥、、、、、/////
    //##########################################################废弃￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥￥、、、、、/////

}